import graphfactory from '../graph/graphfactory'
import LXCOM from '../common/common.js'
import {LGXEnums}  from '../common/sysenum'

var ContainerGraphSvc = {}

export default { ContainerGraphSvc }

ContainerGraphSvc.create = function (dataManger, symbolid, mospt) {
    let graphLst = this.createTemp(dataManger, symbolid, mospt);
    if (graphLst) {
        for (let i = 0; i < graphLst.length; i++) {
            let child = graphLst[i];
            var pNewGraph = dataManger.m_pCurentLayer.addGraph(child);
            if (pNewGraph) {
                dataManger.addCurentNewGraph(child);
            }
        }
    }
    return graphLst;
}

ContainerGraphSvc.createTemp = function (dataManger, symbolid, mospt) {
    let symbol = dataManger.getSymbol(symbolid, 0);
    if (!symbol) {
        return null;
    }

    let containerGraph = graphfactory.GraphFactory.createGraphByType32(symbolid,symbol);
    if (!containerGraph) {
        return null;
    }
    this.asignContainerProp(containerGraph, symbol, mospt);
    let graphLst = [];
    graphLst.push(containerGraph);

    let list = this.createCompositGraphChild(dataManger,symbol, mospt);
    for (let i = 0; i < list.length; i++) {
        let child = list[i];
        if (child.m_graphType == LGXEnums.LGXGraphType.TextGraph_type){
            let parentGraph = child.getParentGraph();
            if (!parentGraph){
                containerGraph.addChild(child);
            }
        }
        else{
            containerGraph.addChild(child);
        }
        graphLst.push(child);
    }
    
    graphLst.forEach(p => {
        p.addObserver(dataManger);
    });
    return graphLst;
}

ContainerGraphSvc.asignContainerProp = function (containerGraph, symbol, worldPos) {
    containerGraph.setSymbol(symbol);
    var position = containerGraph.getPosCopy();
    position.x = worldPos.x;
    position.y = worldPos.y;
    containerGraph.setPos(position);
    containerGraph.calScope(1);
    return;
}

ContainerGraphSvc.createCompositGraphChild = function (dataManger,symbol, mapCntPos) {
    let graphLst = [];
    let graph2UnitMap = [];
    let oldGID2GraphList = [];
    let unitList = symbol.getSymbolUnitLst();
    for (let i = 0; i < unitList.length; i++) {
        let childunit = unitList[i];
        let childSymbol = dataManger.getSymbol(childunit.type, 0);
        let childGraph = graphfactory.GraphFactory.createGraphByType32(childunit.type,childSymbol);
        if (childGraph) {
            childunit.m_nUseful = 1;
            childGraph.assignInfo(childunit);
            childGraph.translate(mapCntPos, 1,true);
            childGraph.calScope(1);
            graphLst.push(childGraph);
            let t = {unit:childunit,graph:childGraph};
            graph2UnitMap.push(t);

            let t1 = {gid:childunit.gid,graph:childGraph};
            oldGID2GraphList.push(t1);
        }
    }
    graph2UnitMap.forEach(t => {
        if (t.graph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
            && t.unit.parentGID != 0){
                let refGraph = null;
                oldGID2GraphList.forEach(p => {
                    if (p.gid == t.unit.parentGID){
                        refGraph = p.graph;
                        return ;
                    }
                });
                if (refGraph){
                    refGraph.setLabel(t.graph);
                }
        }
    });
    return graphLst;
}
